package 华为.text43_迷宫问题;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static int m; //行  y
    public static int n; //行  x    // n 是行, m 是列
    public static int[][] ints;
    public static ArrayList<pos> path=new ArrayList<>();


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){

            // 注意 hasNext 和 hasNextLine 的区别

            n=scanner.nextInt();
            m=scanner.nextInt();

            ints=new int[n][m];

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    ints[i][j]=scanner.nextInt();
                }
            }
            int x=0;
            int y=0;
            ints[0][0]=1; // 把初始位置 标记位已经访问过
            path.add(new pos(0,0));
            dfs(x,y);

        }
    }

    private static void  dfs( int x, int y) {  //  3部曲 , 边界,遍历,递归,回溯
        //边界条件
        if (x==n-1&&y==m-1){
            for (pos item : path) {
                System.out.println(item);
            }
            return;
        }
        //向上
        if (x-1>=0&&ints[x-1][y]==0){
            path.add(new pos(x-1,y));
            // 标记已经访问过
            ints[x-1][y]=1;
            // 递归
            dfs(x-1,y);
            // 回溯: 这里回溯的时候 好像把这个标记去不去没有太大的关系???
            path.remove(path.size()-1);
        }
        //向下
        if (x+1<n&&ints[x+1][y]==0){
            path.add(new pos(x+1,y));
            ints[x+1][y]=1;
            dfs(x+1,y);
            path.remove(path.size()-1);
        }
        //向左
        if (y-1>=0&&ints[x][y-1]==0){
            path.add(new pos(x,y-1));
            ints[x][y-1]=1;
            dfs(x,y-1);
            path.remove(path.size()-1);
        }
        //向右
        if (y+1<m&&ints[x][y+1]==0){
            path.add(new pos(x,y+1));
            ints[x][y+1]=1;
            dfs(x,y+1);
            path.remove(path.size()-1);
        }
    }
}
class pos{
    int x;
    int y;

    public pos(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "(" + x +","+ y + ")";
    }
}

